<template>
  <div class="app-container">
    <div>
      <el-form>
        <el-row class="rowFormItem">
          <el-col :span="3">
            <el-input v-model="keyword" placeholder="姓名/手机" />
          </el-col>
          <el-col :span="5" style="margin: 0 10px;">
            <el-date-picker style="width: 100%;" v-model="timeList" value-format="yyyy-MM-dd" type="daterange"
                            align="right" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
                            :picker-options="pickerOptions">
            </el-date-picker>
          </el-col>
          <!-- 提交按钮 -->
          <el-col :span="5" class="clear">
            <el-form-item style="text-align: center;width: 250px;margin-left: 10px;">
              <el-button type="primary" size="large" @click="handleSubmit()">检索</el-button>
              <el-button type="success" size="large" @click="addmodel()">添加</el-button>
              <el-button type="warning" size="large" style="margin-left: 8px" @click="handleReset()">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <el-table :data="formdata" v-loading="loading" border style="width: 100%" :header-cell-style="{background:'#eef1f6',color:'#606266'}">
      <el-table-column align="center" prop="id" label="序号" />
      <el-table-column align="center" prop="photo_url" label="头像">
        <template slot-scope="scope">
          <el-button type="text" size="medium">
            <img style="width: 50%;" :src="scope.row.user_info.photo_url" />
          </el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="user_info.nickname" label="昵称" />
      <el-table-column align="center" prop="realname" label="姓名" />
      <el-table-column align="center" prop="phone" label="手机" />
      <el-table-column align="center" prop="created_at" label="创建时间" />
      <el-table-column label="操作" align="center" width="300px">
        <template slot-scope="scope">
          <el-button class="btnAppeal" size="small" type="warning" @click="updateMsg(scope.row)">修改</el-button>
          <el-button class="btnAppeal" size="small" type="danger" @click="delMsg(scope.row.id)">删除</el-button>
          <router-link class="btnAppeal" style="margin-left: 10px;margin-top: 10px" :to="{path: 'extension_list',query: {id: scope.row.user_id}}">
            <el-button size="small" type="primary">推广列表</el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div style="text-align: center;margin-top: 20px">
      <el-pagination background layout="prev, pager, next" @current-change="pageChange" :page-size="pageSize"
                     :total="totalusers" />
    </div>

    <!-- 添加模态款 -->
    <el-dialog width="40%" :close-on-click-modal="true" :title="title" :visible.sync="modelMsg">
      <div class="msgDiv">
        <!-- <h3>店铺基础信息</h3> -->
        <div>
          <el-form ref="form" :model="form" label-width="80px">
            <el-form-item v-show="vshow" label="序号">
              <el-input v-model="form.id" disabled></el-input>
            </el-form-item>
            <el-form-item label="姓名" :required="true">
              <el-input style="width: 300px" v-model="form.realname" placeholder="请输入姓名"></el-input>
            </el-form-item>
            <el-form-item label="手机" :required="true">
              <el-input style="width: 300px" type="number" v-model="form.phone" placeholder="请输入手机号码"></el-input>
            </el-form-item>
            <el-form-item label="选择用户" style="text-align: left;">
              <el-select filterable v-model="form.user_id">
                <el-option v-for="(item,index) in userList" :key="index" :value="item.id" :label="item.user_info.nickname"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <el-button type="success" @click="addMsg(form)">{{addORupdate}}</el-button>
        </div>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import ORIGIN from '../../assets/json/origin.json'
  export default {
    data() {
      return {
        ORIGIN,
        modelMsg: false,
        keyword:"",
        form: {
          id: "",
          realname: "",
          phone: "",
          user_id: ""
        },
        vshow: false,
        loading: false,
        title: "",
        addORupdate: "", //添加或修改
        formdata: [],
        totalusers: 0, //总行数
        pageSize: 10, //每页几行
        startpage: 1, //起始页
        userList: [],
        timeList: [],
        start_time: '',
        end_time: '',
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
      }
    },
    mounted() {
      this.getStaffList()
      this.getUserList()
    },
    methods: {

      //获取分页列表
      getUserList() {
        let params = {
          "page": 1,
          "limit": 9999
        }
        //调用后台提供的接口方法
        this.$api.getUserList(params).then(res => {
          if (res.data.level == "success") {
            this.userList = res.data.data
          } else {
            this.$message.error(res.data.message)
          }


        })
      },

      checkPhone(phoneNumb) {
        var user = /^1[0-9]{10}$/
        if (!user.test(phoneNumb)) {
          this.$message({
            message: '请输入正确的手机号码',
            type: 'error'
          });
          return false
        }
        return true
      },
      checkEmail(email) {
        var user = /^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/
        if (!user.test(email)) {
          this.$message({
            message: '请输入正确邮箱格式',
            type: 'error'
          });
          return false
        }
        return true
      },
      //获取分页列表
      getStaffList() {
        this.loading = true
        let params = {
          "page": this.startpage,
          "limit": this.pageSize,
          "keyword":this.keyword,
          "start_time": this.start_time,
          "end_time": this.end_time
        }
        //调用后台提供的接口方法
        this.$api.getStaffList(params).then(res => {
          if(res.data.level=="success"){
            this.formdata = res.data.data
            this.totalusers = res.data.page_info.total
            this.loading = false
          }else{
            this.$message.error(res.data.message)
          }

        })
      },
      handleSubmit(){
        if(this.timeList !=null){
          this.start_time = this.timeList[0]
          this.end_time = this.timeList[1]
        }
        this.getStaffList()
      },
      handleReset(){
        this.keyword=""
        this.end_time=""
        this.start_time=""
        this.timeList = []
        this.startpage = 1
        this.getStaffList()
      },
      //分页 pageChange当前页发生改变触发函数
      async pageChange(page) {
        this.startpage = page
        this.loading = true
        //调用获取列表的方法
        this.getStaffList()
      },
      addmodel() {
        this.modelMsg = true
        this.title = '添加'
        this.addORupdate = "添加"
        this.vshow = false
        this.form.id=""
        this.form.realname= ""
        this.form.phone= ""
        this.form.user_id = ""
      },
      //提交店铺入驻申请
      async addMsg(form) {
        let checkPhone = this.checkPhone(form.phone)

        if(!checkPhone){
          return false
        }
        //判断带星号的是否为空
        /*if ((form.name && form.phone && form.email && form.sex && form.photo_id) == "") {
          this.$message({
            message: "带星号为必填项，请完善信息！",
            type: "warning"
          })
          return false;
        }*/

        /* 显示加载中。。。 */
        const loadingOne = this.$loading({
          lock: true,
          text: '保存中...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })

        if (form.id > 0) {
          /* 修改 */
          console.log(form)
          await this.$api.setStaff(form).then(res => {
            console.log(res)
            this.$message({
              type: res.data.level,
              message: res.data.message
            });
            if (res.data.level == "success") {
              this.getStaffList()
              loadingOne.close()
              this.modelMsg = false
            } else {
              this.modelMsg = true
              loadingOne.close()
            }

          })
        } else {
          /* 添加 */
          console.log(form)
          await this.$api.setStaff(form).then(res => {
            this.$message({
              type: res.data.level,
              message: res.data.message
            });
            if (res.data.level == "success") {
              this.getStaffList()
              loadingOne.close()
              this.modelMsg = false
            } else {
              this.modelMsg = true
              loadingOne.close()
            }

          })
        }
        this.modelMsg = false
      },
      //修改弹出框
      updateMsg(data) {
        console.log(data)
        this.modelMsg = true
        this.title = '修改'
        this.addORupdate = "修改"
        this.vshow = true
        this.form.id = data.id
        this.form.realname= data.realname
        this.form.phone= data.phone
        if(!data.user_id||data.user_id==="0"){
          this.form.user_id = ''
        } else{
          this.form.user_id = parseInt(data.user_id)
        }
      },
      //删除店铺信息
      async delMsg(id) {
        let delid = {
          id: id
        }
        await this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {

          this.$api.delStaff(delid).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            if (res.data.level == "success") {
              this.getStaffList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      //图片上传
      handleAvatarSuccess(res, file) {
        //console.log(res.data.id)
        this.form.photo_id = res.data.id
        //console.log(file)
        this.form.photo_url = res.data.img_url;
      },

    }
  }
</script>
<style scoped="scoped">
  /deep/.el-card__header {
    background-color: #409EFF;
    color: whitesmoke;
  }

  /deep/.el-dialog {
    /*margin-top: 2vh !important;*/
  }

  .msgDiv {
    width: 50%;
    /*margin: auto;*/
    text-align: center;
  }

  .el-input {
    display: flex;
    flex-grow: <2>;
  }
  /deep/.el-textarea__inner{
    min-height: 100px !important;
  }

  /* 图片上传css */
  .avatar-uploader /deep/.el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }

  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
